/* 
 * AUTHOR: Kevin Lam
 */

package com.apps.ubc.cc.ajax;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.apps.services.UBCSectionDetailService;
import com.apps.services.factory.UBCSectionDetailServiceFactory;

public class SeatInformationController extends HttpServlet {
	
	UBCSectionDetailService sds = UBCSectionDetailServiceFactory.getUBCSectionDetailService();
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) {
		String dept = req.getParameter("dept");
		String course = req.getParameter("course");
		String section = req.getParameter("section");
		int value = sds.init(dept, course, section);
		String response = "<seatinformation>\n";
		if(value == 1){
			response += "\t<total>"+sds.getTotalSeats()+"</total>\n";
			response += "\t<generalRemain>"+sds.getGenSeatsRemain()+"</generalRemain>\n";
			response += "\t<restrictedRemain>"+sds.getRestrictSeatsRemain()+"</restrictedRemain>\n";
			response += "\t<registered>"+sds.getRegistered()+"</registered>\n";
		}
		else if(value == 2){
			response += "\t<error>This section is for available for Standard Time Tables only</error>\n";
		} else {
			response += "\t<error>An error occured while obtaining seat information for this section. Check the UBC website for more details.</error>\n";
		}
		response += "</seatinformation>";
		try {
			resp.setContentType("text/xml");
			resp.getWriter().write(response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
